package com.example.myproject.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import javax.mail.internet.InternetAddress;

@Service
public class EmailService {

    @Autowired
    private JavaMailSender mailSender;

    // 注入配置的用户名，通常也是发件人地址
    @Value("${spring.mail.username}")
    private String from;
    public void sendVerificationEmail(String to, String token) {
        String verifyUrl = "http://localhost:8080/api/user/verify?email=" + to + "&token=" + token;
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setSubject("【注册验证】请验证您的邮箱");
        message.setText("请点击以下链接完成邮箱验证：\n" + verifyUrl + "\n链接15分钟内有效。");
        mailSender.send(message);
    }
}